const Koa = require("koa"),
  router = require("koa-router")(),
  render = require("koa-art-template"),
  static = require("koa-static"),
  session = require("koa-session"),
  bodyParser = require('koa-bodyparser'),
  admin = require("./routers/admin"),
  api = require("./routers/api"),
  index = require("./routers/index"),
  sd = require('silly-datetime'),
  jsonp = require('koa-jsonp'),
  bodyparser = require('koa-bodyparser'),
  path = require("path"),
  app = new Koa();

app.use(static(__dirname + "/public"));

app.keys = ["some secret hurr"];

app.use(bodyparser())
const CONFIG = {
  key: "koa:sess",
  maxAge: 864000,
  overwrite: true /** (boolean) can overwrite or not (default true) */,
  httpOnly: true /** (boolean) httpOnly or not (default true) */,
  signed: true /** (boolean) signed or not (default true) */,
  rolling: true /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */,
  renew: false /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
};
app.use(session(CONFIG, app));

app.use(bodyParser());
app.use(jsonp())

render(app, {
  root: path.join(__dirname, "views"),
  extname: ".html",
  debug: process.env.NODE_ENV !== "production",
  dateFormat:dateFormat = function(value){
    return sd.format(value,'YYYY-MM-DD HH:mm')
  }
});

//配置层级路由
router.use("/admin", admin);
//在模块里暴露路由 并且启动
router.use("/api", api);

router.use(index);

app
  .use(router.routes())
  .use(router.allowedMethods())
  .listen(8080);
